package thinkingInJava;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.formula.functions.T;

/**
 * 
 *
 */

public class Learn15 {

	public static void main(String[] args) {
		t2();
	}

	public static void t1() {
		Learn15A<Integer> le = new Learn15A<Integer>(1);
		Learn15A<Learn15> le2 = new Learn15A<Learn15>(new Learn15());
		System.out.println(le.get());
		System.out.println(le2.get());
	}
	public static void t2(){
		Type<Learn15> integer=Init.create(Learn15.class);
		Learn15 i2=integer.next();
		System.out.println(i2);
	}

	@Override
	public String toString() {
		return this.getClass().getName();
	}

}

class Learn15A<T> {
	private T t;

	public Learn15A(T t) {
		this.t = t;
	}

	public T get() {
		return t;
	}
}

/**
 * 这样就可以把不同的对象组成一个新的对象了
 */
class Learn15B<T, A> extends Learn15A<T> {
	private A a;

	public Learn15B(T t, A a) {
		super(t);
		this.a = a;
	}

}

/**
 * 泛型接口
 */
interface Type<T> {
	// 返回你需要的对象
	T next();
}

class Init<T> implements Type<T> {
	private Class<?> type;
	public Init(Class<?> type){
		this.type=type;
	}
	@Override
	public T next() {
		try {
			return (T) type.newInstance();
		} catch (InstantiationException e) {
			
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	public static <T> Type<T> create(Class<?> type){
		return new Init<T>(type);
	}
}

class TypeEntity implements Type<TypeEntity> {

	@Override
	public TypeEntity next() {
		return new TypeEntity();
	}

}

/**
 * 泛型方法
 */
class TypeMethod {
	public void get(T t) {
		System.out.println(t.getClass().getCanonicalName());
	}

	public <T> List<T> set(T... t) {
		List<T> list = new ArrayList<T>();
		for (T t2 : t) {
			list.add(t2);
		}
		return list;
	}
}
